package avicit.im.lantop.basic.lexiconManager.action;

import avicit.cbb.appreg.service.CommSysInfoIntfsService;
import avicit.im.lantop.basic.lexiconManager.service.LantopLexiconService;
import avicit.im.lantop.basic.lexiconManager.service.api.LantopLexiconServiceApi;
import avicit.im.lantop.basic.themewordmanager.domain.LantopThemeWord;
import avicit.im.lantop.useconfig.service.UseSetIntfsService;
import avicit.platform6.api.session.SessionHelper;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.commons.utils.JsonUtils;
import avicit.platform6.core.context.ThreadContextHelper;
import avicit.platform6.core.jdbc.Page;
import avicit.platform6.core.properties.PlatformConstant.OpResult;
import com.github.pagehelper.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>金航数码科技有限责任公司</p>
 * <p>作者：胡光昌</p>
 * <p>邮箱：huguangc@avicit.com</p>
 * <p>创建时间： 2014-10-13 上午9:28:19 </p>
 * <p>类说明：主题词管理模块的控制器类</p>
 * <p>修改记录： </p>
 */
@Controller
@SuppressWarnings({"unused", "unchecked","deprecation" })
@RequestMapping(value = "/LantopLexiconController")
public class LantopLexiconController {

	@Autowired(required = true)
	private LantopLexiconServiceApi themeWordService;

	@Autowired(required = true)
	private UseSetIntfsService intfsService;

	@Autowired(required = true)
	private CommSysInfoIntfsService commSysInfoIntfsService;

	/**
	 * 
	 * 获取主题词的初始化信息和查询方法
	 * 
	 * @author 胡光昌
	 * @param request
	 * @param response
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value = "/getThemeWordInfo")
	@ResponseBody
	public Map<String,Object> getThemeWordInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,Object> map = new HashMap<>();
		String json = request.getParameter("param");
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("filter-LIKE-FORMAL_THEME_WORD",json);
		parameter.put("filter-EQ-ORG_ID", ThreadContextHelper.getOrgId());
		parameter.put("filter-EQ-DEPT_ID", ThreadContextHelper.getDeptId());
		parameter.put("filter-EQ-ATTRIBUTE_01", request.getParameter("falg"));
		String pageNo = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		Page<LantopThemeWord> page = new Page<LantopThemeWord>();
		page.setRows(ComUtil.getPageSize(pageSize));
		page.setPage(ComUtil.getPageNo(pageNo));
		page = themeWordService.getPageList(page, parameter);
		map.put("total", page.getEntityCount());
		map.put("rows", page.getEntities());
		return map;
	}

	/**
	 * 保存数据
	 * 
	 * @author 胡光昌
	 * @throws Exception
	 * @param request
	 * @param response
	 * @return ModelAndView
	 * @throws
	 */
	@RequestMapping(value = "/saveData")
	@ResponseBody
	public Map<String,Object> saveData(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,Object> map = new HashMap<>();

		String dataSting = request.getParameter("datas");
		LantopThemeWord[] themeWordArray = JsonUtils.newInstance().Json2Bean2DataGrid(dataSting, LantopThemeWord[].class);

		for (int i = 0; i < themeWordArray.length; i++) {
			LantopThemeWord themeWord = themeWordArray[i];
			boolean flag = true;
			boolean isUpdate = StringUtils.isBlank(themeWord.getId()) ? false : true;
			if (isUpdate) {
				int count = this.themeWordService.getCount(ThreadContextHelper.getOrgId(),themeWord.getFormalThemeWord(), themeWord.getId());
				if (count == 0) {
					this.themeWordService.updateData(themeWord);
					map.put("flag", OpResult.success);
				} else {
					flag = false;
					map.put("flag", "关键字已存在!");
				}
			} else {
				int count = this.themeWordService.getCount(ThreadContextHelper.getOrgId(),themeWord.getFormalThemeWord(), "");
				if (count == 0) {
					this.themeWordService.insertData(themeWord);
					map.put("flag", OpResult.success);
				} else {
					flag = false;
					map.put("flag", "关键字已存在!");
				}
			}
			// return mav;
		}
		return map;
	}

	/**
	 * 保存自动索引
	 * 
	 * @author 胡光昌
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 *             ModelAndView
	 * @throws
	 */
	@RequestMapping(value = "/saveInfo")
	public ModelAndView saveInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView();
		mav.setView(null);
		String orgId = ThreadContextHelper.getOrgId();
		String deptId = ThreadContextHelper.getDeptId();
		String keyWord = "IS_USE_MARK";
		String keyValue = "1";
		String sysId = "LANTOP";
		String id = this.themeWordService.findAutoThemeSearch(sysId, keyWord,orgId);
		if ("".equals(id)) {
			this.intfsService.createUseSetInfoByIntfs(orgId, deptId, sysId, keyWord, keyValue);
		} else {
			this.themeWordService.updateAutoThemeWord(id);
		}
		return mav;
	}

	/**
	 * 删除数据
	 * 
	 * @author 胡光昌
	 * @param request
	 * @param response
	 * @return ModelAndView
	 * @throws
	 */
	@RequestMapping(value = "/deleteData")
	@ResponseBody
	public Map<String,Object> deleteData(HttpServletRequest request, HttpServletResponse response) {
		Map<String,Object> map = new HashMap<>();
		String ids = request.getParameter("ids");
		try {
			this.themeWordService.deleteThemeWord(ids);
			map.put("flag", OpResult.success.ordinal());
		} catch (Exception e) {
			map.put("flag", OpResult.failure.ordinal());
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 获取页面初始化数据
	 * @param request
	 * @param response
	 * @return ModelAndView
	 * @throws Exception 
	 */
	@RequestMapping(value = "/getConfigInfo")
	public ModelAndView getConfigInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView();
		String sysId = "LANTOP";
		String orgId = ThreadContextHelper.getOrgId();
		String deptId = ThreadContextHelper.getDeptId();
		String value = this.themeWordService.getConfigInfo(sysId, orgId, deptId);
		mav.addObject("value", value);
		mav.setViewName("avicit/im/lantop/basic/themewordmanager/ThemeWordManager");
		return mav;
	}

	/**
	 * 取消自动索引
	 * @param request
	 * @param response
	 * @return ModelAndView
	 * @throws Exception 
	 */
	@RequestMapping(value = "/cancelThemeAutoSearch")
	public ModelAndView cancelThemeAutoSearch(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView();
		String sysId = "LANTOP";
		this.themeWordService.updateIsUseWord(ThreadContextHelper.getOrgId(),sysId, "IS_USE_MARK");
		return mav;
	}

}
